package com.example.wanandroid.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.BaseData;
import com.example.wanandroid.core.bean.main.collect.FeedArticleData;
import com.example.wanandroid.core.bean.project.ProjectListData;
import com.example.wanandroid.ui.activity.ProjectDetailActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ProjectListAdapter extends RecyclerView.Adapter<ProjectListAdapter.MyViewHoder> {

    private String TAG = "ProjectListAdapter";
    List<BaseData> dataList = new ArrayList<>();
    Context context;
    private LayoutInflater mLayoutInflater;
    Object lock = new Object();

    public ProjectListAdapter(Context context){
        this.context=context;
        mLayoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mLayoutInflater.getContext(), R.layout.item_project_list, null);
        MyViewHoder myViewHoder = new MyViewHoder(view);
        return myViewHoder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
        BaseData data = dataList.get(position);
        FeedArticleData data1 = (FeedArticleData)data;
        holder.mTitleTv.setText(data1.getTitle());
        holder.mContent.setText(data1.getDesc());
        holder.mAuthor.setText(data1.getAuthor());
        holder.mTime.setText(data1.getNiceDate());
        holder.mLink.setText(data1.getLink());
        View itemView = holder.itemView;
//
        Glide.with(mLayoutInflater.getContext())
                .load(data1.getEnvelopePic())
                .placeholder(R.mipmap.loading)
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.mImageView.setImageDrawable(resource);
                    }
                });

        holder.mCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(itemView.getContext(), ProjectDetailActivity.class);
                intent.putExtra("link",holder.mLink.getText());
                itemView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView mTitleTv;
        TextView mContent;
        ImageView mImageView;
        TextView mTime;
        TextView mAuthor;
        CardView mCard;
        TextView mLink;

        public MyViewHoder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.item_project_list_iv);
            mTitleTv = itemView.findViewById(R.id.item_project_list_title_tv);
            mContent = itemView.findViewById(R.id.item_project_list_content_tv);
            mTime = itemView.findViewById(R.id.item_project_list_time_tv);
            mAuthor = itemView.findViewById(R.id.item_project_list_author_tv);
            mCard = itemView.findViewById(R.id.project_card);
            mLink = itemView.findViewById(R.id.item_project_list_install_tv);
        }
    }
    public void refreshData(List<BaseData> list){
        synchronized (lock){
            this.dataList = list;
            notifyDataSetChanged();
        }
    }

    public void addData(BaseData data){
        synchronized (lock){
            this.dataList.add(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyItemInserted(this.dataList.size()-1);
        }
    }

    public void addDataList(List<BaseData> data){
        synchronized (lock){
            this.dataList.addAll(data);
            List<BaseData> list = this.dataList.stream().distinct().collect(Collectors.toList());
            this.dataList = list;
            notifyDataSetChanged();
        }
    }
}
